#include "System.hpp"
#include "gpio/GPIO.hpp"
#include "usart/USART.hpp"
#include "string.h"

ST::Pin *led = nullptr;
ST::SerialPort *serial_port = nullptr;
char rx_buffer[2048];
char tx_buffer[2048];
void led_toggle(void){
    led->toggle();
}

void feedback(void){
    if ( serial_port != nullptr ){
        int size = serial_port->available();
        memcpy(tx_buffer,rx_buffer,size);
        serial_port->write(tx_buffer,size);
        serial_port->read(rx_buffer,1024);
    }
}

extern "C"
int main(void){
    serial_port = ST::SerialPort::getInstance(0);
    serial_port->setBaudrate(115200);
    serial_port->setDataBits(8);
    serial_port->setPinmap(0);
    serial_port->setTxMode(ST::SerialPort::TransferMode::Interrupt);
    serial_port->setRxMode(ST::SerialPort::TransferMode::Interrupt);
    serial_port->open();
    
    ST::Port *gpio_a = ST::Port::getInstance(0);
    gpio_a->open();
    led = gpio_a->getPin(1);
    led->setAlternateFunction(false);
    led->setMode(Pin::Mode::Output);
    led->setDriveType(Pin::DriveType::PushPull);
    led->open();
    led->set(true);

    serial_port->addEventCallback(SerialPort::EventType::TransmitComplete,led_toggle);
    serial_port->addEventCallback(SerialPort::EventType::Idle,feedback);
    serial_port->read(rx_buffer,1024);
    char buffer[64];
    while ( true ){
        System::delayMs(1000);
        
    }
}